"""
从序列中移除重复的项目
"""
lst=[1,2,3,3,2,26,8,9]

# lsts=[]
# for i in lst:
#     if i not in lsts:
#         lsts.append(i)
#
# print(lsts)
#
# def dedupe(items):
#     lsts=[]
#     for i in items:
#         if i not in lsts:
#             lsts.append(i)
#     return lsts
#
# print(dedupe(lst))
#
# #使用生成器
#
# def new_dedupe(items):
#     seen=[]
#     for i in items:
#         if i not in seen:
#             yield i
#             seen.append(i)
#
# print(list(new_dedupe(lst)))

#如果序列中包含列表，字典等
def mynew_dedupe(items,key=None):
    seen=set()
    for item in items:
        val=item if key is None else key(item)
        if val not in seen:
            yield item
            seen.add(val)

lst1=[{"x":1,"y":3},{"x":3,"y":4}]

print(list(mynew_dedupe(lst1,key=lambda d:d['x'])))

